<?php

namespace App\Http\Controllers\backend;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

// model
use DB;
use App\Model\backend\ProjectStages;
use App\Model\Backend\ProjectType;
use App\Model\Backend\ProjectTypeRole;
use App\Model\Backend\Template;
use App\Model\Backend\TemplateTask;

class TemplateController extends Controller
{
    function __construct(Request $req){
		
		$session = $req->session()->get('member');
		if($session['gid'] != 1) abort(403);
	}
	
	function index(){
		
		$phase = ProjectStages::get();
		$type = ProjectType::get();

		$type_role = ProjectTypeRole::orderBy('rid','asc')->get();

		$template = Template::get();

		$role = [];
		
		foreach($type_role as $t){
		
			if($t->is_auditor){
				//审核组

				if($t->name == '专家审核')
					// $role[$t->type_id]['check']['id'] = $t->rid;
				//else{
					$role[$t->type_id]['auditor'][$t->rid]['name'] = $t->name;
				//}

			}
			else{ 	
				// 任务角色组
				$role[$t->type_id]['role'][$t->rid]['name'] = $t->name;
			}
			//

			$role['name'][$t->rid] = $t->name;
		}
		
		// echo '<pre>'; print_R($role);exit;

		return view('backend.template.index', compact('phase', 'type', 'role','template'));
	}
	
	
	function show(){
		echo 'welcome';
	}
	
	// 插入或更新模板
	function store(Request $request){
		
		$form = $request->all();
		$project_type_name = ProjectType::find($form['projectTypeIds']);

		$data = [
			'project_type_id' => $request->input('projectTypeIds'),
			'name'	=> $request->input('name'),
			'stages_id' => $request->input('stages'),
			'project_type_name' => $project_type_name->name,
			'ptime' => time()
		];
		
		if( (int) $form['tid']){ //更新
			$tid = (int) $form['tid'];
			
			$delTaskId = trim($form['delTaskId'], ',');
			$taskIdArr = explode(',', $delTaskId);
			
			//更新模板
			DB::table('templates')->where('tid', $tid)->update($data);
			// 删除任务
			DB::table('template_stages_task')->whereIn('task_id', $taskIdArr)->delete();
			
			$parr = explode(',', $data['stages_id']);
			// 阶段循环编辑任务
			foreach($parr as $k => $a){
				if(isset($form['edittaskName'][$a])){
					foreach($form['edittaskName'][$a] as $key => $taskname){
						$data2 = [
							'task_name' => $taskname,
							'level' => $form['editlevel'][$a][$key],
							'type_id' => $form['edittypeId'][$a][$key],
							'role_id' => $form['editroleId'][$a][$key],
							'remark' => $form['editremark'][$a][$key],

							'audit_rank' => $form['editaudit_rank'][$a][$key],

							'money' => $form['editperformance'][$a][$key]
						];
						$task_id = $form['task_id'][$a][$key];
						DB::table('template_stages_task')->where('task_id', $task_id)->update($data2);
					}
				}
			}
		}else{ //插入
			$tid = DB::table('templates')->insertGetId($data);
		}
		
		if($tid){
			$parr = explode(',', $data['stages_id']);
			// 插入新任务
			foreach($parr as $k => $a){
				if(isset($form['taskName'][$a])){
					foreach($form['taskName'][$a] as $key => $taskname){
						$data = [
							'sid' => $a,
							'tid' => $tid,
							'task_name' => $taskname,
							'level' => $form['level'][$a][$key],
							'type_id' => $form['typeId'][$a][$key],
							'role_id' => $form['roleId'][$a][$key],
							'remark' => $form['remark'][$a][$key],

							'audit_rank' => $form['audit_rank'][$a][$key],

							'money' => $form['performance'][$a][$key]
						];
						DB::table('template_stages_task')->insert($data);
					}
				}
			}
		}
		
		echo json_encode(['flag'=>1,'status'=>'success']);
	}
	
	
	// ajax 模板输出
	function getTemplate(Request $request){
		$data = [];
		$tid = (int) $request->input('id');
		if($tid){
			$template = Template::where('tid', $tid)->first();
			if($template){
				$data = [
					'tid' => $template->tid,
					'name' => $template->name,
					'stages_id' => $template->stages_id,
					'project_type_id' => $template->project_type_id,
					'project_type_name' => $template->project_type_name
				];
				
				
				$task = TemplateTask::where('tid', $tid)->orderBy('task_id')->get();
				$data['task'] = '';
				
				foreach($task as $t){
					$data['task'][$t->sid][$t->task_id] = [
						'task_id'	=> $t->task_id,
						'sid'	=> $t->sid,
						'fid'	=> $t->fid,
						'task_name'	=> $t->task_name,
						'type_id'	=> $t->type_id,
						'role_id'	=> $t->role_id,

						'audit_rank'	=> $t->audit_rank,

						'remark'	=> $t->remark,
						'performance'		=> $t->money,
						'level'		=> $t->level
					];
				}
			}
		}
		
		echo json_encode($data);
	}
}
